ns=
kubectl create -n $ns -f - << EOF
apiVersion: kubelatte.io/v1alpha1
kind: Template
metadata:
  name: busybox-template
spec:
  apiVersion: v1
  kind: Pod
  data: |-
    spec:
      containers:
        - image: busybox
          command: ["/bin/sh"]
          args: ["-c", "while true; do echo 'Hi I am busybox sidecar. I was started at' $(date -u); sleep 5;done"]
          name: busybox
          resources: {}
EOF

kubectl create -n $ns -f - << EOF
apiVersion: kubelatte.io/v1alpha1
kind: Trigger
metadata:
  name: busybox-injection-trigger
spec:
  mutationConfigs:
    - name: busybox-injection
      match:
        kinds:
          - apiGroups:
              - ''
            kinds:
              - Pod
        name:
          value: nginx
        annotationSelector:
          matchExpressions:
            - key: busybox-injection
              operator: In
              values:
                - enabled
        operations:
          - CREATE
      updateStrategy: merge
      templateRefs:
        - $ns/busybox-template
EOF



kubectl create -n $ns -f - << EOF
apiVersion: kubelatte.io/v1alpha1
kind: Scope
metadata:
  name: busybox-injection-validation
spec:
  items:
    - name: allowed-containers
      path: spec.containers[*]
      value: .*
      kind: Pod
      apiVersion: v1
    - name: allowed-mutation
      path: keys(metadata.labels)
      value: 'kblt\.mutation\.resource'
      kind: Pod
      apiVersion: v1
EOF

sleep 5
kubectl run nginx --image nginx --annotations busybox-injection=enabled -n $ns
sleep 10
kubectl logs nginx -c busybox --tail=5 -n $ns